/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projetCalendar.Model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@Table(name = "evenements")
@NamedQueries({@NamedQuery(name = "Evenement.findById", query = "SELECT e FROM Evenement e WHERE e.id = :id"), @NamedQuery(name = "Evenement.findByObjet", query = "SELECT e FROM Evenement e WHERE e.objet = :objet"), @NamedQuery(name = "Evenement.findByDateDebut", query = "SELECT e FROM Evenement e WHERE e.dateDebut = :dateDebut"), @NamedQuery(name = "Evenement.findByLieu", query = "SELECT e FROM Evenement e WHERE e.lieu = :lieu")})
public class Evenement implements Serializable {
    public static int DUREE_PAR_DEFAUT = 1;
    
    public String getDateDebutDate() {
        SimpleDateFormat formatDateSortie = new SimpleDateFormat("yyyy-MM-dd");
        if(dateDebut != null) return formatDateSortie.format(dateDebut);
        return "";
    }
    
    public String getDateDebutHeure() {
        SimpleDateFormat formatDateSortie = new SimpleDateFormat("HH:mm");
        if(dateDebut != null) return formatDateSortie.format(dateDebut);
        return "";
    }
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id", nullable = false)
    private Long id;
    @Column(name = "objet", nullable = false)
    private String objet;
    @Column(name = "date_debut", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateDebut;
    @Lob
    @Column(name = "description")
    private String description;
    @Column(name = "lieu")
    private String lieu;

    public Evenement() {
    }

    public Evenement(Long id) {
        this.id = id;
    }

    public Evenement(Long id, String objet, Date dateDebut) {
        this.id = id;
        this.objet = objet;
        this.dateDebut = dateDebut;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getObjet() {
        return objet;
    }

    public void setObjet(String objet) {
        this.objet = objet;
    }

    public Date getDateDebut() {
        return dateDebut;
    }

    public void setDateDebut(Date dateDebut) {
        this.dateDebut = dateDebut;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLieu() {
        return lieu;
    }

    public void setLieu(String lieu) {
        this.lieu = lieu;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Evenement)) {
            return false;
        }
        Evenement other = (Evenement) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "projetCalendar.Model.Evenement[id=" + id + "]";
    }
    
 
    public static Evenement getEvenementHeure(Calendar jourHeure, List<Evenement> evenements) {
        Evenement event = null;
        for(Evenement tempEvent : evenements) {
            Calendar dateDebut = Calendar.getInstance();
            dateDebut.setTime(tempEvent.getDateDebut());
            Calendar dateFin = Calendar.getInstance();
            dateFin.setTime(tempEvent.getDateDebut());
            dateFin.add(Calendar.HOUR_OF_DAY, tempEvent.getDuree());
            if(event == null && dateDebut.getTimeInMillis() <= jourHeure.getTimeInMillis() && dateFin.getTimeInMillis() > jourHeure.getTimeInMillis()) {
                event = tempEvent;
            }
        }
        return event;
    }
    public int getDuree() {
        return 1;
    }
    public static boolean estDispo(Calendar date, List<Evenement> evenements, int duree) {
        boolean dispo = true;
        if(date.get(Calendar.HOUR_OF_DAY) == 23 && date.get(Calendar.MINUTE) == 30) return false;
        Calendar finDate = Calendar.getInstance();
        Calendar finEventDate = Calendar.getInstance();
        Calendar tempDate = Calendar.getInstance();
        Calendar tempEventDate = Calendar.getInstance();
        finDate.setTime(date.getTime());
        finDate.add(Calendar.HOUR_OF_DAY, duree);
        for(Evenement evenement : evenements) {
            tempDate.setTime(date.getTime());
            finEventDate.setTime(evenement.getDateDebut());
            finEventDate.add(Calendar.HOUR_OF_DAY, evenement.getDuree());
            while(tempDate.before(finDate)) {
                tempEventDate.setTime(evenement.getDateDebut());
                while(tempEventDate.before(finEventDate)) {
                    if(tempEventDate.getTimeInMillis() == tempDate.getTimeInMillis()) {
                        dispo = false;
                    }
                    tempEventDate.add(Calendar.MINUTE, 30);
                }
                tempDate.add(Calendar.MINUTE, 30);
            }
        }
        return dispo;
    }

}
